const express = require('express')
const router = express.Router()
const mysql = require('../util/mysql')

// 文章发布
router.post('/', async (req, res, next) => {
  const date = new Date()
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hours = date.getHours()
  const min = date.getMinutes()
  const sec = date.getSeconds()
  const data = year + '-' + month + '-' + day + ' ' + hours + ':' + min + ':' + sec
  const { token, name, txt } = req.body
  const promisePool = mysql.sql()
  const info = await promisePool.query('select uid from user where token=?', [token])
  const uid = info[0][0].uid
  await promisePool.query('insert into art(uid,header,content,date,zan) value(?,?,?,?,?)', [uid, name, txt, data, 0])
  res.send({ code: 1 })
})

// 列表拿到数据
router.get('/info', async (req, res, next) => {
  const token = req.query.token
  const promisePool = mysql.sql()
  const info = await promisePool.query('select uid from user where token=?', [token])
  const uid = info[0][0].uid
  const msg = await promisePool.query('select header,date,look from art where uid = ?', [uid])
  res.send({ code: 1, info: msg[0] })
})

// 文章编辑
router.put('/updata', async (req, res, next) => {
  const { token, header } = req.body.info
  const promisePool = mysql.sql()
  const info = await promisePool.query('select uid from user where token=?', [token])
  const uid = info[0][0].uid
  const msg = await promisePool.query('select header,content,aid from art where uid=? and header=?', [uid, header])
  res.send({ code: 1, info: msg[0][0] })
})

router.post('/updata', async (req, res, next) => {
  console.log(req.body)
  const { token, txt, aid, name } = req.body
  const promisePool = mysql.sql()
  const info = await promisePool.query('select uid from user where token=?', [token])
  const uid = info[0][0].uid
  console.log(aid, uid)
  await promisePool.query('update art set content=?,header=? where uid=? and aid=?', [txt, name, uid, aid])
  res.send({ code: 1 })
})

// 文章删除
router.delete('/updata', async (req, res, next) => {
  const token = req.query.token
  const header = req.query.header
  const promisePool = mysql.sql()
  const info = await promisePool.query('select uid from user where token=?', [token])
  const uid = info[0][0].uid
  await promisePool.query('delete from art where uid=? and header=?', [uid, header])
  res.send({ code: 1 })
})

// 发送评论
router.post('/show', async (req, res, next) => {
  console.log(req.body)
  const { uid, aid, text, token } = req.body
  const promisePool = mysql.sql()
  const info = await promisePool.query('select uid from user where token=?', [token])
  const cid = info[0][0].uid
  await promisePool.query('insert into comment(aid,uid,cid) values(?,?,?)', [aid, uid, cid])
  const date = new Date()
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hours = date.getHours()
  const min = date.getMinutes()
  const sec = date.getSeconds()
  const data = year + '-' + month + '-' + day + ' ' + hours + ':' + min + ':' + sec
  await promisePool.query('insert into message(uid,aid,info,time) values(?,?,?,?)', [uid, aid, text, data])
  res.send({ code: 1 })
})

// 渲染评论表
router.get('/show', async (req, res, next) => {
  const uid = req.query.info.uid
  const aid = req.query.info.aid
  const token = req.query.token
  console.log(uid, aid, token)
  const promisePool = mysql.sql()
  const info = await promisePool.query(`SELECT pho,name,info,time from message join user on message.uid = user.uid 
  where message.aid=? order by time desc `, [aid])
  res.send({ code: 1, msg: info[0] })
})

// 第一次渲染评论列表
// router.get('/list', async (req, res, next) => {
//   const uid = req.query.uid
//   const aid = req.query.aid
//   const promisePool = mysql.sql()
//   const info = await promisePool.query('select info,time,name,pho from message join user on message.uid=user.uid where message.uid=? and aid=?', [uid, aid])
//   res.send({ code: 1, msg: info[0] })
// })

module.exports = router
